package cn.itsource.pethome.system.pet.service.impl;

import cn.itsource.pethome.constants.BaseConstants;
import cn.itsource.pethome.jwt.LoginContextUtils;
import cn.itsource.pethome.system.employee.domain.Employee;
import cn.itsource.pethome.system.employee.mapper.EmployeeMapper;
import cn.itsource.pethome.system.pet.domain.Pet;
import cn.itsource.pethome.system.pet.domain.PetDetail;
import cn.itsource.pethome.system.pet.domain.PetOnlineAuditLog;
import cn.itsource.pethome.system.pet.mapper.PetDetailMapper;
import cn.itsource.pethome.system.pet.mapper.PetMapper;
import cn.itsource.pethome.system.pet.mapper.PetOnlineAuditLogMapper;
import cn.itsource.pethome.system.pet.query.PetQuery;
import cn.itsource.pethome.system.pet.service.IPetService;
import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import cn.itsource.pethome.utils.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2023-02-05
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private LoginContextUtils loginContextUtils;
    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private PetDetailMapper petDetailMapper;
    @Resource
    private PetMapper petMapper;
    @Resource
    private AuthService authService;
    @Resource
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    /**
     *  上下架 状态
     *  创建时间
     *  狗归属的店铺      谁去录入就是谁的店铺id
     *  详细信息 没有关联上
     * @param pet
     */
    @Override
    public void add(Pet pet, HttpServletRequest request) {
        //得到当前登录用户信息
        Employee employee = (Employee)loginContextUtils.getLoginUser(request);
        pet.setState(BaseConstants.Pet.STATE_DOWN);
        pet.setCreatetime(new Date());
        Employee one = employeeMapper.findOne(employee.getId());
        pet.setShopId(one.getShop_id());
        super.add(pet);
        //宠物详细信息
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        petDetailMapper.add(detail);
    }

    @Override
    public void delete(Long id) {
        petDetailMapper.deleteDetailByPetId(id);
        super.delete(id);
    }

    public List<Pet> findQuery(PetQuery query, HttpServletRequest request) {
        getCurrentShopId(query, request);
        return super.findQuery(query);
    }

    //获取当前用户的店铺
    private void getCurrentShopId(PetQuery query, HttpServletRequest request) {
        Employee employee = (Employee)loginContextUtils.getLoginUser(request);
        Long shopId = employee.getShop_id();
        if (shopId != null)
            query.setShopId(shopId);
    }

    public Long countPage(PetQuery query,HttpServletRequest request) {
        getCurrentShopId(query, request);
        return super.countPage(query);
    }

    //上架
    @Override
    public String onsale(List<Long> ids) {
        List<Pet> pets = petMapper.findByIds(ids);
        //去除已经上架的数据
        List<Pet> collect = pets.stream().filter(pet -> pet.getState() == BaseConstants.Pet.STATE_DOWN).collect(Collectors.toList());
        //现在有两种情况  有审核通过(直接上架)  有审核失败的(提示给用户)
        List<Pet> errorPet = new ArrayList<>();

        collect.forEach(pet ->{
            if (authService.getConclusion(pet.getName())){
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(pet.getId());
                petOnlineAuditLog.setAuditTime(new Date());
                petOnlineAuditLog.setState(BaseConstants.Pet.STATE_UP);
                petOnlineAuditLog.setNote("审核通过");
                petOnlineAuditLogMapper.add(petOnlineAuditLog);

                pet.setState(BaseConstants.Pet.STATE_UP);
                pet.setOnsaletime(new Date());
                petMapper.update(pet);
            }else {
                errorPet.add(pet);
            }
        });
        if(errorPet.isEmpty()){
            return "上架成功";
        }
        String errorMsg = "存在非法内容:";
        for (int i = 0; i < errorPet.size(); i++) {
            if(i == 0){
                errorMsg = errorMsg + errorPet.get(i).getName();
            }else {
                errorMsg = errorMsg + "、" + errorPet.get(i).getName();
            }
        }
        return errorMsg;
    }

    @Override
    public void offsale(List<Long> ids) {
        petMapper.offsale(ids);
    }

    @Override
    public PetDetail getDetail(Long id) {
        return petDetailMapper.findByPetId(id);
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        petDetailMapper.update(detail);
    }
}
